#include <stdio.h>
#include <errno.h>
#include <sys/types.h>         
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <strings.h>



#include "employee_client.h"
#define errlog(errmsg) do{\
							perror(errmsg);\
							printf("%s----%s----%d\n",__FILE__,__func__,__LINE__);\
							exit(1);\
						 }while(0)
							

int main(int argc, const char *argv[])
{
	int option;
	int sockfd;    //sock文件描述符
	struct sockaddr_in clientaddr;	//客户端信息填充结构体
	socklen_t clientaddrlen;         //clientaddrlen 大小



 	// 	客户端与服务端连接部分
	
	if(argc < 3){
		printf("您输入的参数 << 3,请重新输入......\n");
		//goto please_input;
		exit(1);
	}
	
	//创建sock 填充sock结构体  发送连接请求
	
	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0){
		errlog("fail to create sockfd");
	}
	
	clientaddr.sin_family = AF_INET;
	clientaddr.sin_port = htons(atoi(argv[2]));
	clientaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	clientaddrlen = sizeof(struct sockaddr_in);
	if(connect(sockfd, (const struct sockaddr *)&clientaddr,clientaddrlen) < 0){
		errlog("fail to bind...");
	}
	
	//连接测试成功...... 进行注册 和 登陆 
	
	frame_func();
     
	while(1){

		printf("请输入：");
		scanf("%d",&option);
		if(option == 2){

			employee_register();

			if((recv(sockfd,&client_block,sizeof(client_block),0)) < 0){
				errlog("register fail to recv......");
			}

			printf("%s\n",client.e_msg.msg);//接收服务器端的注册成功信息

			goto log_in; 

		}else if(option == 1){

log_in:

			printf("***********员工登录(1)*****管理员登录(2)**********\n");
			scanf("%d",&option);
			if(option == 1){
				employee_log_in();//员工登录
			}
			if(option == 2){
				admin_log_in();//管理员登录
			}


		}else {

			while(!getchar());
		}

	}
	
	close(sockfd);
	return 0;
}
